package com.macro.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.macro.mall.common.utils.HttpKit;
import com.macro.mall.service.JdyService;
import com.macro.mall.utils.JsonDateValueProcessor;
import com.macro.mall.vo.JdyProductCategoryVO;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class JdyServiceImpl implements JdyService {
    @Autowired
    private Environment env;
    @Override
    public List<JdyProductCategoryVO> productCategory() {
        String s = HttpKit.get("https://api.kingdee.com/jdyscm/productCategory/list?access_token="+token().get("access_token")+"&dbId=79849829157&pageSize=10000&page=1");
        Map<String,Object> res = JSON.parseObject(s, Map.class);
        System.out.println("------------------"+res.get("items").toString());
        return JSON.parseArray( res.get("items").toString(),JdyProductCategoryVO.class);
    }

    @Override
    public Map<String, Object> token() {
        String s = HttpKit.get("https://api.kingdee.com/auth/user/access_token?client_id="+env.getProperty("jdy.clientId")+"&client_secret="+env.getProperty("jdy.clientSecret")+"&username="+env.getProperty("jdy.username")+"&password="+env.getProperty("jdy.password"));
        Map<String,Object> ress = JSON.parseObject(s, Map.class);
        return (Map<String, Object>) ress.get("data");
    }

    @Override
    public Map<String, Object> productList(String productNumber,String categoryId,Integer pageSize,Integer page) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> productNumberMap= new HashMap<>();
        if (productNumber!=null){
            productNumberMap.put("productNumber",productNumber);
        }
        productNumberMap.put("status",0);
        System.out.println("类别ID"+categoryId);
        if (categoryId!=null){
            productNumberMap.put("categoryId",categoryId);
        }
        if (pageSize!=null){
            productNumberMap.put("pageSize",pageSize);
        }
        if (page!=null){
            productNumberMap.put("page",page);
        }
        map.put("filter",productNumberMap);
        String data = JSONObject.fromObject(map).toString();
        System.out.println(data);
        String res1 = HttpKit.post("https://api.kingdee.com/jdyscm/product/list?access_token="+token().get("access_token")+"&dbId="+env.getProperty("jdy.dbId"),data,headers);
        System.out.println(res1);
        return JSON.parseObject(res1, Map.class);
    }

    @Override
    public Map<String, Object> purchaseAdd(String productNumber) {
        Map<String, Object> product = productList(productNumber, null, null, null);
        if ((int)product.get("code")==46002){
            return null;
        }
        System.out.println("product====="+product);
        List<Map<String, Object>> productItems = (List<Map<String, Object>>) product.get("items");
        System.out.println("productItems==="+productItems);
        List<Map<String, Object>> skus = (List<Map<String, Object>>) productItems.get(0).get("invSku");
        if ((int)productItems.get(0).get("isWarranty")==0) {
            System.out.println("ssssss");
            return null;
        }
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyyMMdd" );
        SimpleDateFormat sdf1 =   new SimpleDateFormat( "yyyy-MM-dd" );
        Map<String,Object> purchase = new HashMap<>();
        List<Map<String,Object>> items = new ArrayList<>();
        Map<String,Object> data = new HashMap<>();
        List<Map<String,Object>> entriesList = new ArrayList<>();
        data.put("number","");
        data.put("date",sdf.format(new Date()));
        data.put("supplierNumber",env.getProperty("jdy.supplierNumber"));
        if (CollUtil.isNotEmpty(skus)){
            for (Map<String, Object> sku: skus){
                Map<String,Object> entries = new HashMap<>();
                entries.put("productNumber",productItems.get(0).get("productNumber"));
                entries.put("location",env.getProperty("jdy.entries.location"));
                entries.put("qty",1);
                entries.put("price",sku.get("elsPurPrice"));
                entries.put("unit",productItems.get(0).get("unit"));
                entries.put("skuCode",sku.get("skuNumber"));
                entries.put("remark","萨特商城使用");
                entries.put("prodDate",sdf1.format(new Date()));
                entries.put("batch",1);
                entriesList.add(entries);
            }
        }else{
            Map<String,Object> entries = new HashMap<>();
            entries.put("productNumber",productItems.get(0).get("productNumber"));
            entries.put("location",env.getProperty("jdy.entries.location"));
            entries.put("qty",1);
            entries.put("price",productItems.get(0).get("elsPurPrice"));
            entries.put("unit",productItems.get(0).get("unit"));
            //entries.put("skuCode",sku.get("skuNumber"));
            entries.put("remark","萨特商城使用");
            entries.put("prodDate",sdf1.format(new Date()));
            entries.put("batch",1);
            entriesList.add(entries);
        }
        data.put("entries",entriesList);
        items.add(data);
        purchase.put("items",items);
        System.out.println("purchase==="+purchase);
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
        String d = JSONObject.fromObject(purchase,jsonConfig).toString();
        String res= HttpKit.post("https://api.kingdee.com/jdyscm/purchase/add?access_token="+token().get("access_token")+"&dbId="+env.getProperty("jdy.dbId"),d,headers);
        Map<String, Object> ress = JSON.parseObject(res,Map.class);
        System.out.println("110==="+ress);
        if ((int)ress.get("code")!=0){
            System.out.println(ress);
        }
        purchaseList(ress);
        return ress;
    }

    @Override
    public Map<String, Object> purchaseList(Map<String, Object> purchaseRes) {
        List<Map<String,Object>> items=(List<Map<String,Object>>) purchaseRes.get("items");
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        map.put("checkStatus",0);
        map.put("supplierNumber",env.getProperty("jdy.supplierNumber"));
        map.put("createName",items.get(0).get("creator"));
        map.put("updTimeBegin",items.get(0).get("updateTime"));
        map.put("pageSize",1000);
        map.put("page",1);
        data.put("filter",map);
        System.out.println("data==="+data);
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
        String d = JSONObject.fromObject(data,jsonConfig).toString();
        String res= HttpKit.post("https://api.kingdee.com/jdyscm/purchase/list?access_token="+token().get("access_token")+"&dbId="+env.getProperty("jdy.dbId"),d,headers);
        Map<String, Object> ress = JSON.parseObject(res,Map.class);
        System.out.println("135==="+ress);
        List<Map<String,Object>> resList = (List<Map<String,Object>>) ress.get("items");
        String billNo = "";
        for (Map<String,Object> s: resList){
            billNo+=s.get("number")+",";
        }
        billNo = billNo.substring(0,billNo.length() - 1);
        billCheck(billNo);
        return ress;
    }

    @Override
    public Map<String, Object> warrantyList(String number) {
        String warranty= HttpKit.get("https://api.kingdee.com/jdyscm/warranty/list?access_token="+token().get("access_token")+"&dbId="+env.getProperty("jdy.dbId")+"&batch=1&number="+number);
        return JSON.parseObject(warranty, Map.class);
    }

    @Override
    public Map<String, Object> billCheck(String billNo) {
        String check= HttpKit.get("https://api.kingdee.com/jdyscm/bill/check?access_token="+token().get("access_token")+"&dbId="+env.getProperty("jdy.dbId")+"&billNo="+billNo+"&billType=pur&state=1");
        System.out.println("审核="+JSON.parseObject(check, Map.class));
        return JSON.parseObject(check, Map.class);
    }
}
